import shutil
import os
import numpy as np
import scipy.io as sio

path = './Resultados_bootstrap'

try:
    os.mkdir(path)
    print("Folder %s created!" % path)
except FileExistsError:
    print("Folder %s already exists" % path)

# code_time0 = [0]*101
# code_time1 = [0]*101
for i in range(1,101):
    shutil.copy2('batch_eh0_comp0_time0/eh0_comp0_time0_boot'+str(i)+'/ParEst_eh0_time0_Mex_nokids.mat', path +'/ParEst_eh0_time0_Mex_nokids_boot'+str(i)+'.mat')
    shutil.copy2('batch_eh0_comp0_time1/eh0_comp0_time1_boot'+str(i)+'/ParEst_eh0_time1_Mex_nokids.mat', path +'/ParEst_eh0_time1_Mex_nokids_boot'+str(i)+'.mat')
    # code_time0[i] = 'load ParEst_eh0_time0_Mex_boot'+str(i) +' ParEst_eh0_time0_Mex_boot'+str(i)+'\n'
    # code_time1[i] = 'load ParEst_eh0_time1_Mex_boot'+str(i) +' ParEst_eh0_time1_Mex_boot'+str(i)+'\n'

# code_time0[0] = '\n'
# code_time1[0] = '\n'


# with open(path+'/'+'carrega_resultados_time0.m', 'x', encoding='utf-8') as file1:
#     file1.writelines(code_time0)

# with open(path+'/'+'carrega_resultados_time1.m', 'x', encoding='utf-8') as file2:
#     file2.writelines(code_time1)
    
###############################################################################    
##junta todos os resultados em um arquivo .mat unico (ParEst_eh0_time0_Mex_nokids_allboots.mat e ParEst_eh0_time1_Mex_nokids_allboots.mat)

matrix_t0 = np.zeros((100,24))
matrix_t1 = np.zeros((100,5))

for j in range(1,101):

    x_t0 = sio.loadmat(path + '/ParEst_eh0_time0_Mex_nokids_boot'+str(j))
    x_t0 = x_t0['ParEst_eh0_time0_Mex_nokids']
    matrix_t0[j-1] = x_t0
    
    x_t1 = sio.loadmat(path + '/ParEst_eh0_time1_Mex_nokids_boot'+str(i))
    x_t1 = x_t1['ParEst_eh0_time1_Mex_nokids']
    matrix_t1[j-1] = x_t1
    
mdic_t0 = {'__header__': b'MATLAB 5.0 MAT-file, Platform: MACI64, Created on: Sat Aug 12 00:23:53 2023',
 '__version__': '1.0',
 '__globals__': [],
 'ParEst_eh0_time0_Mex_nokids_allboots': matrix_t0}
    
mdic_t1 = {'__header__': b'MATLAB 5.0 MAT-file, Platform: MACI64, Created on: Wed Aug 16 13:21:28 2023',
 '__version__': '1.0',
 '__globals__': [],
 'ParEst_eh0_time1_Mex_nokids_allboots': matrix_t1}

sio.savemat('ParEst_eh0_time0_Mex_nokids_allboots.mat', mdic_t0)
sio.savemat('ParEst_eh0_time1_Mex_nokids_allboots.mat', mdic_t1)